<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="typeOrProfessional" placeholder="请输入证书类别/专业" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查 询</el-button>
        <el-button @click="getDataListReset()">重 置</el-button>
        <el-button type="primary" plain @click="addOrUpdateHandle()">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      :max-height="tableHeight"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
      type="index"
      header-align="center"
      align="center"
      label="#"
      >
      </el-table-column>
      <el-table-column
        prop="typeName"
        header-align="center"
        align="center"
        min-width="180"
        label="证书类型">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        min-width="200"
        label="证书类别">
      </el-table-column>
      <el-table-column
        prop="professionalName"
        header-align="center"
        align="center"
        min-width="160"
        label="证书专业">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        min-width="120"
        align="center"
        label="描述">
      </el-table-column>
      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        min-width="60"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        min-width="80"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">无效</el-tag>
          <el-tag v-else size="small">有效</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        min-width="90"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" style="color:#53b4ff;" @click="addOrUpdateHandleModification(scope.row)">修改</el-button>
          <el-button type="text" size="small" style="color:#fc8e2d;" @click="deleteHandleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 20, 50, 100, 200]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增-->
    <el-dialog
    title="新增"
    width="50%"
    :visible.sync="transactionVisible"
    >
      <el-form :model="certificateForm" class="formStyle" :rules="certificateRule" ref="certificateForm" label-width="80px">

        <el-row>

            <el-col :span="24" v-if="moduleTag == ''" class="radioStyle">
              <el-form-item label="类型">
                    <el-radio v-model="certificateForm.saveTag" label="2">证书专业</el-radio>
                    <el-radio v-model="certificateForm.saveTag" label="1">证书类别</el-radio>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="证书类型" prop="pIdType">
              <el-select v-model="certificateForm.pIdType" filterable placeholder="请选择证书类型" style="width:100%" @change="pIdChange">
                      <el-option
                        v-for="item in pIdTypeList"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code">
                      </el-option>
                  </el-select>
              </el-form-item>
            </el-col>

            <el-col v-if="certificateForm.saveTag == 2" :span="24">
              <el-form-item label="证书类别" prop="idType">
              <el-select v-model="certificateForm.idType" filterable placeholder="请选择证书类型" style="width:100%">
                      <el-option
                        v-for="item in idTypeList"
                        :key="item.name"
                        :label="item.name"
                        :value="item.name">
                      </el-option>
                  </el-select>
              </el-form-item>
            </el-col>

            <el-col v-if="certificateForm.saveTag == 2" :span="24">
              <el-form-item label="证书专业" prop="nameA">
                  <el-input v-model="certificateForm.nameA" placeholder="请填写证书专业"></el-input>
              </el-form-item>
            </el-col>

            <el-col v-if="certificateForm.saveTag == 1" :span="24">
              <el-form-item label="证书类别" prop="nameB">
                  <el-input v-model="certificateForm.nameB" placeholder="请填写证书类别"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="描述">
                  <el-input v-model="certificateForm.remark" placeholder="请填写描述信息"></el-input>
              </el-form-item>
            </el-col>

              <el-col :span="24">
              <el-form-item label="排序">
                   <el-input v-model="certificateForm.sort" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="请填写描述"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="状态" prop="status">
              <el-select v-model="certificateForm.status" filterable placeholder="请选择证书状态" style="width:100%">
                      <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.name"
                        :value="item.value">
                      </el-option>
                  </el-select>
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="button-see" size="mini" plain @click="transactionVisible = false">取 消</el-button>
        <el-button class="button-see" size="mini" type="primary" @click="transactionFormSubmit()">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          type: ''
        },
        alterId:'',
        typeOrProfessional:'',
        dataList: [],
        pIdTypeList:[],
        idTypeList:[],
        certificateList:[],
        certificateForm:{
        remark:'',
        sort:1,
        status:1,
        saveTag:'2',
        pIdType:'',
        idType:'',
        nameA:'',
        nameB:'',
        remark:''
        },
        moduleTag:'',
        modificationForm:{
        remark:'',
        num:1,
        radio:'1'
        },
        statusList:[
          {value: 1, name:'有效'},
          {value: 0, name:'无效'}
        ],
        pageIndex: 1,
        pageSize: 20,
        totalPage: 0,
        dataListLoading: false,
        transactionVisible:false,
        modificationVisible:false,
        tableHeight:0,
        heightis: 100,
        addOrUpdateVisible: false,
        certificateRule: {
          pIdType: [
            { required: true, message: '请选择证书类型', trigger: 'blur' }
          ],
          idType: [
            { required: true, message: '请选择证书类别', trigger: 'blur' }
          ],
          status:[
             { required: true, message: '请选择状态', trigger: 'blur' }
          ],
          nameA:[
             { required: true, message: '请填写证书专业', trigger: 'blur' }
          ],
          nameB:[
             { required: true, message: '请填写证书类别', trigger: 'blur' }
          ],
        },
      }
    },
    created () {
      this.getDataList()
      this.tackHeight()
      this.getPidType()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/certificate/professional/list'),
          method: 'post',
          params: this.$http.adornParams({
            'typeOrProfessional':this.typeOrProfessional,
            'page': this.pageIndex,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            console.log(this.dataList)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      tackHeight(){
      this.heightis = 180
      this.adjustTableHeight()
      },
      adjustTableHeight(){
      this.$nextTick(() => {
          console.log(window.innerHeight);
          this.tableHeight = window.innerHeight - this.heightis;
            //后面的50：根据需求空出的高度，自行调整
          console.log(this.tableHeight);
        })
      },
      getPidType(){
        this.$http({
          url: this.$http.adornUrl('/sys/certificate/professional/types'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0) {
              this.pIdTypeList = data.data
              this.certificateForm.pIdType = this.pIdTypeList[0].code
              if(this.certificateForm.pIdType !== ''){
               this.pIdChange()
              }
          }
          else {
          }
        })
      },
      pIdChange(){
        this.$http({
        url: this.$http.adornUrl('/sys/certificate/professional/categories/' + this.certificateForm.pIdType),
        method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0) {
              this.idTypeList = data.data
          }
          else {
          }
        })
      },
      transactionFormSubmit(){
          this.dataListLoading = true
          if(this.certificateForm.saveTag == 1){
          var name = this.certificateForm.nameB
          var type = this.certificateForm.pIdType
          }
          if(this.certificateForm.saveTag == 2){
          var name = this.certificateForm.nameA
          var type = this.certificateForm.idType
          }
// 新增
        if(this.moduleTag == ''){
        this.$refs['certificateForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/certificate/professional/save`),
              method: 'post',
              data: this.$http.adornData({
                'name':name,
                'remark':this.certificateForm.remark,
                'saveTag':this.certificateForm.saveTag,
                'sort':this.certificateForm.sort,
                'status':this.certificateForm.status,
                'type':type
              })
            }).then(({data}) => {

              if (data && data.code === 0) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                  this.dataListLoading = false
                  this.transactionVisible = false
                  this.certificateForm.remark = ''
                  this.certificateForm.nameA = ''
                  this.certificateForm.nameB = ''
                  this.getDataList()
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.dataListLoading = false
              }
            })
          }
        })
        }else{
            // 修改
        this.dataListLoading = true
        this.$refs['certificateForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/certificate/professional/update`),
              method: 'post',
              data: this.$http.adornData({
                'id':this.alterId,
                'name':name,
                'remark':this.certificateForm.remark,
                'saveTag':this.certificateForm.saveTag,
                'sort':this.certificateForm.sort,
                'status':this.certificateForm.status,
                'type':type
              })
            }).then(({data}) => {

              if (data && data.code === 0) {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                  this.dataListLoading = false
                  this.transactionVisible = false
                  this.certificateForm.remark = ''
                  this.certificateForm.nameA = ''
                  this.certificateForm.nameB = ''
                  this.getDataList()
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.certificateForm.remark = ''
                this.certificateForm.nameA = ''
                this.certificateForm.nameB = ''
                this.dataListLoading = false
              }
            })
          }
        })
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
        // 新增
      addOrUpdateHandle (id) {
        this.moduleTag = ''
        this.certificateForm.remark = ''
        this.certificateForm.nameA = ''
        this.certificateForm.nameB = ''
        this.pIdChange()
        this.transactionVisible = true
      },
    //   xiugai
     addOrUpdateHandleModification(row){
    //  证书类别
     if(row.moduleTag == 'conf_certificate_type_category'){
      this.alterId = row.id
      this.certificateForm.saveTag = '1'
      this.moduleTag = row.moduleTag
      this.certificateForm = {...this.certificateForm,...row}
      this.certificateForm.nameB = row.name
      this.certificateForm.pIdType = row.typeCode
      this.transactionVisible = true
     }
    //  证书专业
     if(row.moduleTag == 'conf_certificate_type_professional' ){
      this.alterId = row.id
      this.certificateForm.saveTag = '2'
      this.moduleTag = row.moduleTag
      this.certificateForm = {...this.certificateForm,...row}
      this.certificateForm.pIdType = row.typeCode
      this.certificateForm.nameA = row.name
      this.certificateForm.idType = row.categoryName
      this.pIdChange()
      this.transactionVisible = true
     }
     },
      // 删除
      deleteHandleDelete (id) {
          this.$http({
            url: this.$http.adornUrl('/sys/certificate/professional/delete/' + id),
            method: 'get',
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      },
      getDataListReset(){
        this.typeOrProfessional = ''
        this.getDataList()
      }
    }
  }
</script>

<style lang="scss" scoped>

/deep/
.el-dialog__header{
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
.el-dialog__title{
  font-size: 16px !important;
  font-weight: 700 !important;
  padding: 20px !important;
}
}
.formStyle{
    padding-left: 100px;
    padding-right: 100px;
}
.paixu{
    width: 100%;
}
.button-see{
  font-size: 14px;
}

.mod-config{
    padding: 15px;
}
</style>